package com.hsnn.medstgmini.pro.base.service.Impl;

import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hsnn.medstgmini.Constants;
import com.hsnn.medstgmini.dao.HibernateSqlUtil;
import com.hsnn.medstgmini.pro.base.dao.BaseShortageDao;
import com.hsnn.medstgmini.pro.base.model.BaseShortage;
import com.hsnn.medstgmini.pro.base.service.BaseShortageService;
import com.hsnn.medstgmini.util.ConstantsUtil;
import com.hsnn.medstgmini.util.Pagination;
import com.hsnn.medstgmini.util.ParseNumber;

/**
 * @category 缺货申报
 * @author 应晓川
 * @date 2015年11月27日10:07:01
 */
@Service
public class BaseShortageServiceImpl implements BaseShortageService {

	@Autowired
	private BaseShortageDao shortageDao;
	
	@Autowired
	private HibernateSqlUtil dao;

	@Override
	public void addShortage(BaseShortage baseShortage) {
		dao.add(baseShortage);
	}

	@Override
	public void updateShortage(BaseShortage baseShortage) {
		dao.update(baseShortage);
	}
	

	@Override
	public Pagination getShortageInfo(Pagination page) {
		return shortageDao.getShortageInfo(page);
	}
	
	@Override
	public Pagination getShortageList(Pagination page) {
		return shortageDao.getShortageList(page);
	}

	@Override
	public Pagination getDrugqualityspecListByHosp(Pagination page) {
		page = shortageDao.getDrugqualityspecListByHosp(page);
		List<HashMap<String,Object>> list = (List<HashMap<String, Object>>) page.getRows();
		Date now =new Date();
		HashMap<String,Object> map =null;
		Date tempTime =new Date();
		for(int i=0,temp =list.size();i<temp;i++){
			boolean tag = true;
			 map = list.get(i);
			  tempTime =(Date) map.get("submitDatetime");
			if(tempTime!=null){
				  Calendar c = Calendar.getInstance();  
				  c.setTimeInMillis(tempTime.getTime());
				  c.add(Calendar.DATE, 30);
				  Date date =new Date(c.getTimeInMillis());
				tag = date.before(now);
			} 
			map.put("isdeclare", tag);	
		}
		return page ;
	}

	
	@Override
	public Pagination getFeedbackListByHosp(Pagination page) {
		page = shortageDao.getFeedbackListByHosp(page);
		 List<HashMap<String,Object>> list = (List<HashMap<String, Object>>) page.getRows();
			HashMap<String,Object> map =null;
			StringBuffer status=new StringBuffer();
		
			for(int i=0,temp =list.size();i<temp;i++){
				String statusColor ="";
				status.delete( 0, status.length());
				map =list.get(i);
				Integer isReadedInt =Integer.parseInt(map.get("is_readed")==null?"":map.get("is_readed").toString());
				map.put("is_readed", isReadedInt==Constants.HAVE_READ?"已阅读":"未阅读");
				Object burAuditStatus = map.get("bur_audit_status");
				if(null!=burAuditStatus){
					status.append("卫生局");
					status.append(ConstantsUtil.shortageStatus(burAuditStatus.toString()));
					int tag = Integer.parseInt(burAuditStatus.toString());
					if(tag==2){
						statusColor="red";
					}
				}else{
					Object cetStatus =  map.get("cet_audit_status");
					int tag = Integer.parseInt(cetStatus.toString());
					status.append("中心");
					status.append(ConstantsUtil.shortageStatus(cetStatus.toString()));
					if(tag==2){
						statusColor="red";
					}
				}
				map.put("statusColor",statusColor);
				map.put("status",status.toString());
			}
			return page ;
	}

 
	@Override
	public Object getBaseShortageById(String shortageId) {
		return  shortageDao.getBaseShortageById(shortageId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public Pagination getAuditList(Pagination page) {
		page =  shortageDao.getAuditList(page);
		String zxAuditTag=page.getConditions().get("zxAuditTag");
		String heaBurId=page.getConditions().get("heaBurId");
		List<HashMap<String,Object>> list = (List<HashMap<String, Object>>) page.getRows();
		HashMap<String,Object> map =null;
		String status="";
		Integer auditStatus=null;
		for(int i=0,temp =list.size();i<temp;i++){
			map =list.get(i);
			if(StringUtils.isNotBlank(zxAuditTag)){
				 auditStatus =Integer.parseInt(map.get("cet_audit_status")==null?"":map.get("cet_audit_status").toString());
			}
			if(StringUtils.isNotBlank(heaBurId)){
				 auditStatus =Integer.parseInt(map.get("bur_audit_status")==null?"":map.get("bur_audit_status").toString());
			}
			 status=auditStatus==0?"未核实":(auditStatus==1?"核实缺货":(auditStatus==2?"核实未缺货":"其他"));
			map.put("status", status);
		}
		return page;
	}

	 
	@Override
	public void updateByWSJ(BaseShortage baseShortage) {
		 shortageDao.updateByWSJ(baseShortage);
	}
	@Override
	public void updateByZX(BaseShortage baseShortage) {
		 shortageDao.updateByZX(baseShortage);
	}

 
}
